package com.nd.qa.shareplate.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ApkFileUtil {

	public String getApkPackageName(String apkPath) {
		String packageName = "";
		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(String.format("aapt dump badging \"%s\"",
					apkPath));
			InputStream stdout = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(stdout);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null) {
				// System.out.println(line);
				if (line.contains("package:")) {
					System.out.println(line);
					packageName = line.split("'")[1];
					break;
				}
			}
			int exitVal = proc.waitFor();
			System.out.println("Process exitValue: " + exitVal);
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return packageName;
	}

	public String getApkLaunchActivity(String apkPath) {
		String LaunchActivity = "";
		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(String.format("aapt dump badging \"%s\"",
					apkPath));
			InputStream stdout = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(stdout);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null) {
				// System.out.println(line);
				if (line.contains("launchable-activity:")) {
					LaunchActivity = line.split("'")[1];
					System.out.println(LaunchActivity);
					break;
				}
			}
			int exitVal = proc.waitFor();
			System.out.println("Process exitValue: " + exitVal);
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return LaunchActivity;
	}
}
